package pl.kosmowska.ewelina.client.views.widgets;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.CategoryDataRequest;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.CategoryItemDataProxy;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanDialog;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent.Selected;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEventHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SimpleHtmlSanitizer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

public abstract class CategoryEditItemWidget extends Composite {

	public static enum State {
		VIEW, EDIT;
	}
	private State stateIs;


	private static CategoryEditItemWidgetUiBinder uiBinder = GWT.create(CategoryEditItemWidgetUiBinder.class);
	@UiField TextBox tbNameBox;
	@UiField Label tbNameLbl;
	@UiField Image bDelete;
	@UiField Image editImg;
	@UiField FocusPanel pFocus;

	interface CategoryEditItemWidgetUiBinder extends UiBinder<Widget, CategoryEditItemWidget> {
	}

	private CategoryItemDataProxy categoryItemData;
	private BooleanDialog wconfirm;
	TreeItem treeRoot;
	TreeItem widgetTreeItem;
	KeyDownHandler keyHandler;


	public CategoryEditItemWidget(TreeItem mailPanelRoot) {
		initWidget(uiBinder.createAndBindUi(this));

		treeRoot=mailPanelRoot;
		setTitle("Podaj nazwę kategorii");
		bDelete.setTitle("Usuń kategorię");
		if(keyHandler==null){
			keyHandler = new KeyDownHandler() {
				@Override
				public void onKeyDown(KeyDownEvent event) {
					if(KeyCodes.KEY_ENTER == event.getNativeKeyCode()) {
						tbNameLbl.setText(tbNameBox.getText());
						setStateView();
					//	fireChange();
					}
				}
			};
			tbNameBox.addKeyDownHandler(keyHandler);
		}
	}

	public void setData(int i, CategoryItemDataProxy itemData) {
		this.categoryItemData = itemData;
		if(categoryItemData!=null){
			tbNameLbl.setText(categoryItemData.getName());
			tbNameBox.setText(categoryItemData.getName());
			setStateView();	    
		}else{
			setStateEdit();
		}
		if(categoryItemData.isDefaultCategory()){
			bDelete.setVisible(false);
			editImg.setVisible(false);
		}
	}

	public CategoryItemDataProxy getData(CategoryDataRequest request) {
		if (categoryItemData == null) {
			categoryItemData = request.create(CategoryItemDataProxy.class);
		} else {
			categoryItemData = request.edit(categoryItemData);
		}
		categoryItemData.setName(getName());
		return categoryItemData;
	}

	private String getName() {
		String s = tbNameLbl.getText().trim();
		if (s.length() == 0) {
			s="";
		}
		return s;
	}

	public void draw() {
		setStateView();
		drawName();
	}

	private void drawName() {
		if (categoryItemData == null || 
				categoryItemData.getName() == null || 
				categoryItemData.getName().trim().length() == 0) {
			return;
		}

		String s = categoryItemData.getName();
		SafeHtml sh = SimpleHtmlSanitizer.sanitizeHtml(s);
		tbNameBox.setText(sh.asString());
		tbNameBox.setText(sh.asString());
	}

	private void delete() {
		if (wconfirm == null) {
			wconfirm = new BooleanDialog(ViewMessages.CONFIRM_DELETE);
			wconfirm.addSelectionHandler(new BooleanEventHandler() {
				public void onBooleanEvent(BooleanEvent event) {
					if (event.getBooleanEvent() == Selected.TRUE) {
						deleteIt();
					} else if (event.getBooleanEvent() == Selected.FALSE) {
						// do nothing
					}
				}
			});
		}
		wconfirm.center();
	}

	public abstract void deleteIt();


	private void setStateView() {
		stateIs=State.VIEW;
		tbNameBox.setVisible(false);
		tbNameLbl.setVisible(true);
	}

	private void setStateEdit() {
		stateIs=State.EDIT;
		tbNameBox.setVisible(true);
		tbNameLbl.setVisible(false);
	}

	private void fireChange() {
		NativeEvent nativeEvent = Document.get().createChangeEvent();
		ChangeEvent.fireNativeEvent(nativeEvent, this);
	}

	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		return addDomHandler(handler, ChangeEvent.getType());
	}


	@UiHandler("bDelete")
	public void onBDeleteClick(ClickEvent event) {
		delete();
	}


	public void setTreeItem(TreeItem treeItem) {
		widgetTreeItem=treeItem;

	}

	public void removeFromTree(){
		treeRoot.removeItem(widgetTreeItem);
	}

	@UiHandler("editImg")
	void onEditImgClick(ClickEvent event) {
		if(stateIs== State.VIEW)
			setStateEdit();
		else
			setStateView();
	}

	public CategoryItemDataProxy getCategoryItemData() {
		return categoryItemData;
	}

	
}
